
import Themes.HalloweenTheme;
import Themes.ChristmasTheme;

import java.awt.Color;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 * CSC 251 0001 
 * @author William Murcray
 */
public class TicTacToe extends JFrame
{
    public static JMenuBar menuBar;
    public static JMenu themesMenu;
    public static JPanel boardPanel, winLossPanel, leftPanel, panel1, textPanel, panel3;
    public static JTextArea textArea;
    public static JTextField winText, lossText;
    public static JScrollPane scroll;
    public static JLabel winsLabel, lossesLabel;
    public static JRadioButtonMenuItem boring, halloween, christmas;
    public static JButton sendButton, newGameButton;
    public static JButton[] tttButtons;
    public static JPanel[] tttPanels;
    public static int[][] board;
    
    int turn = 0;
    private int myScore = 0, yourScore = 0;
    /**
     * Constructor, adds 2 panels, 1 of which is the board
     */
    public TicTacToe()
    {
        setTitle("Tic Tac Toe");
        setSize(600,300);
        setLayout(new GridLayout(1, 2));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        buildMenuBar();
        buildBoard();
        buildLeftPanel();
        
        
        add(leftPanel);
        add(boardPanel);
        setVisible(true);
    }
    /**
     * Create a menu bar, so far it only has a themes tab
     */
    private void buildMenuBar()
    {
        menuBar = new JMenuBar();
        
        buildThemesMenu();
        
        menuBar.add(themesMenu);
        setJMenuBar(menuBar);
    }
    /**
     * Current themes for the  JMenu  are Boring, Halloween, and Christmas
     */
    private void buildThemesMenu()
    {
        themesMenu = new JMenu("Themes");
        themesMenu.setMnemonic(KeyEvent.VK_T);
        
        boring = new JRadioButtonMenuItem("Boring", true);
        halloween = new JRadioButtonMenuItem("Halloween");
        christmas = new JRadioButtonMenuItem("Christmas");
        
        ButtonGroup bg = new ButtonGroup();
        bg.add(boring);
        bg.add(halloween);
        bg.add(christmas);
        
//        boring.addActionListener(new themeListener());
//        //setSelected
//        halloween.addActionListener(new themeListener());
//        christmas.addActionListener(new themeListener());
        themesMenu.add(boring);
        themesMenu.add(halloween);
        themesMenu.add(christmas);
    }   
    /**
     * This is the action listener for my themesMenu on the MenuBar
     * JRadioButtonMenuItems are boring, Halloween, and Christmas
     */
    private class themeListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if(boring.isSelected())
            {
                //new BoringTheme();
            }
            if(halloween.isSelected())
            {
                HalloweenTheme h = new HalloweenTheme();
            }
            if(christmas.isSelected())
            {
                new ChristmasTheme();
            }
        }
    }
    /**
     * Game Board creates 9 buttons on a 3X3 grid
     */
    private void buildBoard()
    {
        boardPanel = new JPanel();
        boardPanel.setSize(300, 300);
        boardPanel.setLayout(new GridLayout(3, 3));
        
        tttButtons = new JButton[9];
        tttPanels = new JPanel[9];
        
        for(int index = 0; index < tttButtons.length; index++) 
        {
            tttButtons[index] = new JButton();            
            
            tttButtons[index].setBackground(Color.LIGHT_GRAY);
            tttButtons[index].addActionListener(new ButtonActionListener());            
            
            boardPanel.add(tttButtons[index]);            
        }
    }
    private void buildLeftPanel()
    {
        leftPanel = new JPanel();
        leftPanel.setSize(300, 300);
        
        buildWinLossLabel();
        buildTextPanel();
        buildPanel3();
        
        leftPanel.add(winLossPanel);
        leftPanel.add(textPanel);
        leftPanel.add(panel3);
    }
    /*
     * Trying stuff out, trial and error, would like to keep track of 
     * wins/losses later
     * Components added to this panel: 2 labels, JTextArea, and 2 buttons
     */
    private void buildWinLossLabel()
    {
        winLossPanel = new JPanel();
        winLossPanel.setSize(300, 300);
        
        winLossPanel.setLayout(new GridLayout(2, 2));
        winsLabel = new JLabel("WINS");
        winsLabel.setFont(new Font("DIALOG", Font.BOLD, 24));
        winsLabel.setPreferredSize(null);
        lossesLabel = new JLabel("LOSSES");
        lossesLabel.setFont(new Font("DIALOG", Font.BOLD, 24));
        
        winText = new JTextField(2);
        lossText = new JTextField(2);
        winText.setEnabled(false);
        lossText.setEnabled(false);
        
        winLossPanel.add(winsLabel);
        winLossPanel.add(lossesLabel);
        winLossPanel.add(winText);
        winLossPanel.add(lossText);
    }
    
    private void buildTextPanel()
    {
        textPanel = new JPanel();
                
        textArea = new JTextArea(5, 20);
        textArea.setEditable(true);
        textArea.setToolTipText("Type to send a message");
        scroll = new JScrollPane(textArea);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        textArea.setLineWrap(true);
        
        textPanel.add(textArea);
    }
    public void buildPanel3()
    {
        panel3 = new JPanel();
        
        sendButton = new JButton("SEND");
        
        newGameButton = new JButton("NEW GAME");
        newGameButton.setMnemonic(KeyEvent.VK_N);
        newGameButton.setToolTipText("ALT_N To Play Again");
        newGameButton.addActionListener(new newGameButtonListener());
        
        panel3.add(sendButton);
        panel3.add(newGameButton);
    }
    
    private int isGameOver()
    {
        //check if the game is over...
        return 0;
    }
            
    
    /**
     * 
     */
    private class newGameButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            
            for(JButton button : tttButtons) 
            {
                button.setText(null);
                button.setEnabled(true);
                button.setBackground(Color.LIGHT_GRAY);  
            }
        }
    }        
    /**
     *
     */    
    private class ButtonActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            String letter;
            turn++;
            Color backGround, foreGround = null;
            Font font;
            
            if(turn % 2 == 1)
            {
                letter = "X";
                //if you want a different font, change it in here...
                font = new Font("Serif", Font.BOLD + Font.ITALIC, 72);
                if(halloween.isSelected()) 
                {
                    backGround = Color.ORANGE;
                    foreGround = Color.BLUE;
                }
                else if(christmas.isSelected()) 
                {
                    backGround = Color.GREEN;
                }
                else 
                { 
                    backGround = Color.LIGHT_GRAY;
                }
            } 
            else 
            {
                letter = "O";
                font = new Font("Serif", Font.BOLD + Font.ITALIC, 72);
                if(halloween.isSelected()) {
                    backGround = Color.YELLOW;
                }
                else if(christmas.isSelected()) 
                {
                    backGround = Color.RED;
                }
                else 
                { 
                    backGround = Color.DARK_GRAY;
                }
            }            
            if(ae.getSource() instanceof JButton) 
            {
                JButton selectedButton = (JButton)ae.getSource();
                selectedButton.setText(letter);
                selectedButton.setFont(font);
                selectedButton.setBackground(backGround);
                selectedButton.setEnabled(false); 
            }
            int winner = isGameOver();
            if(winner == 3)
            {
                //do somthing... x won
            }
            else if (winner == -3)
            {
                //do somthing... y won
            }
            else
            {
                //do notihng, no one won yet
            }
        }        
    }              
}
